package com.dream.system.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_Lable;
import com.dream.system.service.ISysClass;
import com.dream.system.service.ISysClassPlant;
import com.dream.system.service.ISysLable;
import com.dream.system.service.ISysUser;

@Controller
@RequestMapping("/edu/sys_LableAction.do")
@SuppressWarnings("all")
public class Sys_LableAction extends SuperController {

	@Autowired
	private ISysUser sysUserImpl;
	@Autowired
	private ISysClass sysClassServiceImpl;
	@Autowired
	private ISysClassPlant sysClassPlantImpl;

	@Autowired
	private ISysLable sysLableService;

	/**
	 * 增加一个标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addSysLable")
	public void addSysLable(Sys_Lable model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			Msg msg = this.getMsg("0", "没有登陆!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if ("2".equals(sysUser.getUserType()) || "5".equals(sysUser.getUserType())) {
			if ("2".equals(sysUser.getUserType())) {
				model.setType("2");
			} else {
				model.setType("5");
			}
			if (null == model.getLablePid() || "".equals(model.getLablePid())) {
				model.setLablePid("0");
			}
		} else {
			Msg msg = this.getMsg("0", "只要老师和专家才能增加标签!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		model.setSysUser(sysUser);
		// Map map = this.sysUserImpl.getTeacherClass(model);
		int map = this.sysLableService.addSysLable(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询所有标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getAllLable")
	public void getAllLable(Sys_Lable model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysLableService.getAllLable(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_lable);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}
	
	/**
	 * 查询所有标签树，树只能是专家的
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getAllLableTree")
	public void getAllLableTree(Sys_Lable model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysLableService.getAllLableTree(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_lable);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询所有标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deleteLable")
	public void deleteLable(Sys_Lable model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int map = this.sysLableService.deleteLable(model);
		Msg msg = this.getMsg("1", "操作成功!", null);
		response.getWriter().write(JSONObject.fromObject(msg).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 更新一个标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateSysLable")
	public void updateSysLable(Sys_Lable model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		// Map map = this.sysUserImpl.getTeacherClass(model);
		int map = this.sysLableService.updateSysLable(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 增加一个标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addSysLable1")
	public void addSysLable1(Sys_Lable model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			Msg msg = this.getMsg("0", "没有登陆!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if ("2".equals(sysUser.getUserType()) || "5".equals(sysUser.getUserType())) {
			if ("2".equals(sysUser.getUserType())) {
				model.setType("2");
			} else {
				model.setType("5");
			}
			if (null == model.getLablePid() || "".equals(model.getLablePid())) {
				model.setLablePid("0");
			}
		} else {
			Msg msg = this.getMsg("0", "只要老师和专家才能增加标签!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		 model.setSysUser(sysUser);
		// Map map = this.sysUserImpl.getTeacherClass(model);
		 Sys_Lable map = this.sysLableService.addSysLable1(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		 if(map == null){
			    response.getWriter().write("{}");
				response.getWriter().close();
				response.getWriter().flush();
		 }else{
				JsonConfig jc = new JsonConfig();
			  	jc.setExcludes(JsonString.sys_lable); 
				//Msg msg = this.getMsg("1", "操作成功!", null);
				response.getWriter().write(JSONObject.fromObject(map,jc).toString());
				response.getWriter().close();
				response.getWriter().flush();
		 }
	}
	
}
